package com.yc.sole.system.controller;

import com.yc.sole.framework.mybatis.page.BasePageReq;
import com.yc.sole.system.request.DictItemQueryReq;
import com.yc.sole.framework.mybatis.page.BaseBatchIdsReq;
import com.yc.sole.framework.mybatis.page.Paging;
import com.yc.sole.framework.result.Result;
import com.yc.sole.framework.validate.DeleteGroup;
import com.yc.sole.framework.validate.SaveGroup;
import com.yc.sole.framework.validate.SelectGroup;
import com.yc.sole.framework.validate.UpdateGroup;
import com.yc.sole.framework.web.PreAuth;
import com.yc.sole.system.biz.DictItemBiz;
import com.yc.sole.system.request.DictItemReq;
import com.yc.sole.system.response.DictItemResp;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 字典项管理
 *
 * @author yizuomin
 * @date 2023-03-26
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
public class DictItemController {

    private final DictItemBiz dictItemBiz;

    /**
     * 新增
     *
     * @param dictItemReq
     * @return RestResult
     */
    @PreAuth(value = "system:dict:save", description = "新增字典项")
    @PostMapping("/api/system/dictItem/saveDictItem")
    public Result<Void> saveDictItem(@RequestBody @Validated(SaveGroup.class) DictItemReq dictItemReq) {
        dictItemBiz.saveDictItem(dictItemReq);
        return Result.success();
    }

    /**
     * 通过Id修改
     *
     * @param dictItemReq
     * @return RestResult
     */
    @PreAuth(value = "system:dict:update", description = "修改字典项")
    @PostMapping("/api/system/dictItem/updateDictItemById")
    public Result<Void> updateDictItemById(@RequestBody @Validated(UpdateGroup.class) DictItemReq dictItemReq) {
        dictItemBiz.updateDictItemById(dictItemReq);
        return Result.success();
    }

    /**
     * 通过id删除
     *
     * @param baseDeleteReq
     * @return RestResult
     */
    @PreAuth(value = "system:dict:delete", description = "删除字典项")
    @PostMapping("/api/system/dictItem/deleteDictItem")
    public Result<Void> deleteDictItem(@RequestBody @Validated(DeleteGroup.class) BaseBatchIdsReq baseDeleteReq) {
        dictItemBiz.deleteDictItem(baseDeleteReq.getIds());
        return Result.success();
    }

    /**
     * 通过id查询字典项
     *
     * @param id
     * @return RestResult
     */
    @PreAuth(value = "system:dict:select", description = "通过id查询字典项")
    @PostMapping("/api/system/dictItem/selectDictItemById/{id}")
    public Result<DictItemResp> selectDictItemById(@PathVariable Long id) {
        return Result.data(dictItemBiz.selectDictItemById(id));
    }

    /**
     * 分页查询
     *
     * @param basePageReq
     * @return Result
     */
    @PreAuth(value = "system:dict:select", description = "分页查询字典项")
    @PostMapping("/api/system/dictItem/selectDictItemPage")
    public Result<Paging<DictItemResp>> selectDictItemPage(@RequestBody @Validated(SelectGroup.class) BasePageReq<DictItemQueryReq> basePageReq) {
        return Result.data(dictItemBiz.selectDictItemPage(basePageReq.toPage(), basePageReq.getQuery()));
    }
}
